package com.ms.p2p.service.impl;


import com.ms.common.bean.Message;
import com.ms.common.utils.MsDateUtils;
import com.ms.common.utils.SendMail;
import com.ms.p2p.bean.ReturnSms;
import com.ms.p2p.entity.member.SmsMessage;

import com.ms.p2p.producter.Producter;
import com.ms.p2p.provider.SettingProvider;
import com.ms.p2p.provider.SmsRecordProvider;
import com.ms.p2p.queen.Email;
import com.ms.p2p.queen.SMS;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 短信
 */
@Service
public class SendServiceImpl{
	protected static Logger logger = Logger.getLogger(SendServiceImpl.class);
	@Autowired
	private SettingServiceImpl settingService;
	@Autowired
	private SmsRecordServiceImpl smsRecordService;
	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private Producter producter;
	/**
	 * 发送短信，交给activemq
	 */

	public void sendMail(String email,String subject,String content)  {
		String host = settingService.findByCode("email_host").getText();
		String mailname = settingService.findByCode("email_email")
				.getText();
		String mailpass = settingService.findByCode("email_pwd")
				.getText();
		String from = settingService.findByCode("email_email").getText();
		String to = email;
		String nickname = settingService.findByCode("email_from_name")
				.getText();
		Email emailqueen = new Email();
		emailqueen.setHost(host);
		emailqueen.setMailname(mailname);
		emailqueen.setMailpass(mailpass);
		emailqueen.setFrom(from);
		emailqueen.setTo(to);
		emailqueen.setSubject(subject);
		emailqueen.setContent(content);
		emailqueen.setNickname(nickname);
		producter.SendMail(emailqueen);
	}


	/**
	 * 发送短信，处理返回码，判读是否成功
	 */

	public Message sendSMS(String mobile, String content,  String type, Integer userId) {
		try {
			// 判断网站是否开启了发送短信的开关con_SMS_send
			String openSendMsg = settingService.findValueByCode("SMS_send");
			if (openSendMsg.equals("1")) {
				// 接口提交地址
				String smsUrl = settingService.findValueByCode("SMS_url");
				String smsUserId = settingService.findValueByCode("SMS_userid");
				String smsUsername = settingService.findValueByCode("SMS_account");
				String smsPwd = settingService.findValueByCode("SMS_password");
				String webname = settingService.findValueByCode("webname");
				StringBuilder url = new StringBuilder(smsUrl);
				url.append("?action=send&userid=").append(smsUserId);
				url.append("&account=").append(smsUsername);
				url.append("&password=").append(smsPwd);
				url.append("&content=").append(content);
				url.append("&mobile=").append(mobile);
				SmsMessage smsRecord = new SmsMessage();
				smsRecord.setAction(type);
				smsRecord.setPhone_number(mobile);
				smsRecord.setMessage(content);
				smsRecord.setSend_time(MsDateUtils.getTime());
				if (userId != null && userId!= 0) {
					smsRecord.setUser_id(userId);
				}
				smsRecordService.insert(smsRecord);
				SMS sms = new SMS();
				sms.setUrl(url.toString());;
				sms.setSmsMessage(smsRecord);
				producter.sendSMS(sms);
				return Message.success("短信发送成功！");

			}
			return Message.error("短信发送功能已关闭，请联系客服！");

		} catch (Exception e) {
			logger.error("短信发送失败", e);
			return Message.error("短信发送失败！");
		}
	}
}
